/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_TRACKBALL_H
#define GENERIK_TRACKBALL_H

/** \file trackball.h
This file implements a trackball algorithm.
*/

#include "math.h"

namespace generik
{

	/** A generik trackball implementation.
	This collects scaled 2d coordinates (such as mouse clicks and drags on a window)
	and transforms them into a 3d rotation.
	It is window system agnostic, requiring only the scaled coordinates.
	These should ideally be scaled before passing them to this class, so that
	they lie in the aspect-corrected -1.0:1.0 range
	*/
	struct trackball
	: axisangle
	{
		generik::translation current;
		
		trackball()
		{}
		
		generik::translation project(double x, double y)
		{
			generik::translation result;
			result.x = x;
			result.y = y;
			// use the distance (radius from 0) in a height function:
			double r = x*x + y*y;
//#define TESTTRACKBALL
#ifndef TESTTRACKBALL
			// we are on a gaussian function
			double c = 0.5;
			result.z = exp(-(r*r)/(2*c*c));
#else
			result.z = 0.0;
#endif
			return result;
		}
		
		void init(double x, double y)
		{
			// clear the rotation
			set(0.0, 1.0, 0.0, 0.0);
			// get the current vector
			current = project(x, y);
		}
		
		void update(double x, double y)
		{
			generik::translation p = project(x, y);
			// this gives us a rotation axis.
			// the angle between the two projections gives us the rotation angle
			// now we simply multiply the resulting rotation into the camera.
			generik::translation axis = cross(p, current);
			axis.normalise(); // normalise the axis (since it probably won't be at the moment)
			double ang = generik::angle(p, current);
			set(ang, axis.x, axis.y, axis.z);
			// store the current projection for the next call
			current = p;
		}
	};

}

#endif












